﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotaHelp.Models;
using DotaHelp.Helpers;
using MvcReCaptcha;

namespace DotaHelp.Controllers
{
    public class ForumController : Controller
    {
        DotaModel model = new DotaModel();

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            model.Dispose();
            
        }


        public ActionResult List(int? page, string orderBy)
        {
            var topics = model.Topics.Where(p => !p.IsDeleted && !p.IsNews);
            return View(topics.ByPage(page, orderBy, p => p.Raiting, p => p.Date, ViewData, HttpContext).ToArray());
        }

        public ActionResult Add(bool isNews = false)
        {
			ViewBag.IsNews = isNews;
            ViewBag.UserName = Request.GetUserName(); 
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [CaptchaValidator]
        public ActionResult Add(string author, string password, string caption, bool isNews, string text)
        {
            var t = new Topic()
            {
                IpAddress = Request.ServerVariables["REMOTE_ADDR"],
                Date = DateTime.UtcNow.NowByAstana(),
                Raiting = 0,
                Author = ControllerContext.HttpContext.PrepareUserName(author),
                Caption = caption,
                Text = text,
                Password = password,
				IsNews = isNews
            };

            model.Topics.AddObject(t);
            model.SaveChanges();
            return Content(Url.RouteUrl("Topic", new { id = t.Id }));
        }

        [Authorize(Roles="Admin")]
        public void Delete(int id)
        {
            var p = model.Topics.First(i => i.Id == id);
            p.IsDeleted = true;
            model.SaveChanges();           
        }

        [Sitemap(typeof(Topic), "Date")]
        public ActionResult Details(int id)
        {
            return View(model.Topics.First(i => i.Id == id));
        }

        [ChildActionOnly]
        public string Rating(int id, bool isPlus)
        {
            var topic = model.Topics.First(p => p.Id == id);
            topic.Raiting += isPlus ? 1 : -1;
            model.SaveChanges();
            return topic.Raiting.ToString();
        }

        public ActionResult Edit(int id, string password)
        {
            var topic = model.Topics.First(p=>p.Id == id);
            if (topic.Password != password)
            {
                throw new MessageException("Какой-то другой пароль!"); 
            }

            return View(topic);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, string author, string password, string caption, bool isNews, string text)
        {
            var topic = model.Topics.First(p => p.Id == id);
            if (topic.Password != password)
            {
				throw new MessageException("Какой-то другой пароль!");
            }

            model.UpdateFieldLogs.AddObject(new UpdateFieldLog()
            {
                Date = DateTime.UtcNow.NowByAstana(),
                LastValue = topic.Text,
                NewValue = text,
                FieldName = "Text",
                TableName = "Topic",
                UserName = ControllerContext.HttpContext.PrepareUserName(author),
				RowId = id
            });

            topic.IpAddress = Request.ServerVariables["REMOTE_ADDR"];
            topic.Date = DateTime.UtcNow.NowByAstana();            
            topic.Author = ControllerContext.HttpContext.PrepareUserName(author);
            topic.Caption = caption;
            topic.Text = text;
			topic.IsNews = isNews;
          
            model.SaveChanges();
            return RedirectToRoute("Topic", new { id = topic.Id });
        }

		[ChildActionOnly]
		public ActionResult One(Topic topic)
		{
			if (topic.IsNews)
			{
				var url = Url.RouteUrl("Topic", new { id = topic.Id });
				ViewBag.Comments = model.Comments.Where(u => u.Url == url && !u.IsDeleted).ToArray();
			}
			return PartialView(topic);
		}
    }
}
